<template>
  <div>
    <a-modal v-model="visible"
             width="800px"
             :closable="false"
             :maskClosable="false"
             :title="title" @ok="cancel" @cancel="handleOk" cancelText="确认" okText="取消">
      <a-space direction="vertical">
        <p>{{ message }}</p>
        <a-checkbox v-model="applyToAllInstances">
          把所有发布版本的流程定义下所有在途流程实例都<span style="color: red">{{ isSuspend ? '挂起' : '激活' }}</span>
        </a-checkbox>
      </a-space>
    </a-modal>
  </div>
</template>
<script>
export default {
  name: 'ProcessSuspendOrActive',
  data () {
    return {
      title: '',
      message: '',
      currentModelId: '',
      applyToAllInstances: false,
      isSuspend: true,
      visible: false
    }
  },
  methods: {
    cancel () {
      this.visible = false
    },
    handleOk () {
      if (this.isSuspend) {
        this.$put('/process/design/suspend', {
          modelId: this.currentModelId,
          allCurrentActiveInstances: this.applyToAllInstances
        }).then((res) => {
          if (res.data.status) {
            this.$message.success(res.data.message)
            this.$emit('success')
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        this.$put('/process/design/active', {
          modelId: this.currentModelId,
          allCurrentActiveInstances: this.applyToAllInstances
        }).then((res) => {
          if (res.data.status) {
            this.$message.success(res.data.message)
            this.$emit('success')
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
      this.visible = false
    },
    suspend (record) {
      if (record.deployStatus === undefined || record.deployStatus === '未发布') {
        this.$message.warning('未发布的流程定义不可挂起')
        return
      }
      this.visible = true
      this.applyToAllInstances = false
      this.currentModelId = record.id
      this.title = '挂起流程定义'
      this.message = '确定挂起流程定义【' + record.name + '，' + record.definitionKey + '】的所有发布版本？'
      this.isSuspend = true
    },
    active (record) {
      if (record.deployStatus === undefined || record.deployStatus === '未发布') {
        this.$message.warning('未发布的流程定义不可激活')
        return
      }
      this.visible = true
      this.applyToAllInstances = false
      this.currentModelId = record.id
      this.title = '激活流程定义'
      this.message = '确定激活流程定义:【' + record.name + '，' + record.definitionKey + '】的所有发布版本？'
      this.isSuspend = false
    }
  }
}
</script>
